<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class pembelian extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->model('crud/m_toko');
        $this->load->model('crud/m_detail_order');
        $this->load->model('crud/m_detail_faktur');
        $this->load->model('crud/m_order_barang');
        $this->load->model('crud/m_faktur');
        $this->load->model('crud/m_suplier');
        $this->load->model('crud/m_bukti_transaksi');
        $this->load->model('crud/m_barang');
        $this->load->library("PHPExcel/iofactory");
    }

    function index() {
        $this->rencana();
    }

    function rencana() {
        $this->auth->is_logged_in('form/home', 3);
        $this->base->head_html('Rencana Pembelian');
        //$data['toko'] = $this->m_toko->get();
        $this->load->view('pengurus/br-06/rencana');
    }

    function realisasi() {
        $this->auth->is_logged_in('form/home', 3);
        $this->base->head_html('Realisasi Pembelian');
        $data['toko'] = $this->m_toko->get();
        $this->load->view('pengurus/br-06/realisasi', $data);
    }

    function faktur() {
        $this->auth->is_logged_in('form/home', 3);
        $this->base->head_html('Faktur Pembelian');
        $data['toko'] = $this->m_toko->get();
        $this->load->view('pengurus/br-06/faktur', $data);
    }

    function retur() {
        $this->auth->is_logged_in('form/home', 3);
        $this->base->head_html('Retur Pembelian');
        $data['toko'] = $this->m_toko->get();
        $this->load->view('pengurus/br-06/retur', $data);
    }

    function do_realisasi(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();

        $id_order = $this->input->post('ID_ORDER', TRUE);
        $id = $this->input->post('NOMER_FAKTUR', TRUE);
        $result = $this->m_faktur->get_one($id);
        if($result->num_rows() > 0)
        {
            $this->response->status = "FAKTUR-EXIST";
            $this->response->data = $result->result();
        }
        else
        {
            $this->response->status = "FAKTUR-OK";
            $id_faktur = $this->m_faktur->insert();
            $this->m_order_barang->realisasi($id_order,$id_faktur);
            $this->response->data = $id_faktur;
            $this->response->data = $this->m_detail_order->realisasi($id_order,$id_faktur);
        }

        echo json_encode($this->response);
    }

    function add_order_brg($id_order){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $result = $this->m_detail_order->insert($id_order);
        $this->response->data = $result;
        echo json_encode($this->response);
    }

    function add_faktur_brg($id_faktur){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $result = $this->m_detail_faktur->insert($id_faktur);
        $this->response->data = $result;
        echo json_encode($this->response);
    }

    function add_order(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $nomer = $this->input->post('NOMER_ORDER', TRUE);
        $result = $this->m_order_barang->get_id($nomer);
        
        if($result->num_rows() > 0)
        {
            //$idsuplier = $result->result()->ID_SUPLIER;
            //$namasuplier = $this->m_suplier->get_one($idsuplier);
            $row = $result->row();
            if ($row->ID_FAKTUR != 0){
                $this->response->status = "REALISASI";
                $this->response->data = $result->result();
            }else{
                $this->response->status = "EXIST";
                $this->response->data = $result->result();
            }
            //$this->response->suplier = $namasuplier->result()->NAMA_SUPLIER;
        }
        else
        {
            $this->response->status = "OK";
            $this->response->data = $this->m_order_barang->insert();
        }
        echo json_encode($this->response);
    }

    function delete_order(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $id = $this->input->post('ID_ORDER', TRUE);
        $return = $this->m_order_barang->delete_order($id);
        if ($return) $this->response->status = "OKE";
        echo json_encode($this->response);
    }

    function add_faktur(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $id = $this->input->post('NOMER_FAKTUR', TRUE);
        $result = $this->m_faktur->get_one($id);
        
        if($result->num_rows() > 0)
        {
            $row = $result->row();
            if ($row->ID_BUKTI_TRANSAKSI == 0){
                $this->response->status = "EXIST";
                $this->response->data = $result->result();
            }
            else {
                $this->response->status = "REAL";
                $this->response->data = $result->result();
            }
        }
        else
        {
            $this->response->status = "OK";
            $this->response->data = $this->m_faktur->insert();
        }
        
        echo json_encode($this->response);
    }

    function add_stok_barang($id_faktur){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        
        // add barang
        $result = $this->m_detail_faktur->get("ID_FAKTUR = $id_faktur");
        foreach($result->result() as $row){
            $this->m_barang->addstok_barang($row->ID_BARANG,$row->JUMLAH);
        }

        echo json_encode($this->response);
    }

    function genPO(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $this->response->data = ($this->m_order_barang->getNomerOrder()->row()->ID_ORDER)+1;
        echo json_encode($this->response);

    }

    function realisasi_po(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();

        $id_order = $this->input->post('ID_ORDER', TRUE);
        $id_faktur = $this->input->post('ID_FAKTUR', TRUE);
        $id_bukti = $this->input->post('ID_BUKTI', TRUE);
        
        //$this->m_order_barang->realisasi($id_order,$id_faktur);
        $this->m_faktur->assign_bukti($id_bukti,$id_faktur);
        $this->response->data = $this->m_order_barang->realisasi($id_order,$id_faktur);

        // add barang
        /*
        $result = $this->m_detail_faktur->get("ID_FAKTUR = $id_faktur");
        foreach($result->result() as $row){
            $this->m_barang->addstok_barang($row->ID_BARANG,$row->JUMLAH);
        }    
        */
        echo json_encode($this->response);
    }

    function get_bukti(){
        $this->auth->is_logged_in('form/home', 3);
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_bukti_transaksi->getAll();
        echo json_encode($this->response);
    }

    function cetakPO($idorder){
        //$idorder = $this->input->post("ID_ORDER");
        //$no_order = $this->input->post("NO_ORDER");

        // get detail order
        $resultdetail = $this->m_detail_order->get("ID_ORDER = $idorder");

        // get info order barang
        $order = $this->m_order_barang->getInfoOrder($idorder)->row();

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Purchase Order")
                ->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);


        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A11')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A11')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A11'), 'B11:G11' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "Tlp.: 0542-427723, Fac.: 0542-427722");
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:G1');
        $sheet->mergeCells('A2:G2');
        $sheet->mergeCells('A3:G3');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('A5', "Kepada Yth. Tuan/Nyonya:");
        $objPHPExcel->getActiveSheet()->setCellValue('F5', "Nomer : $order->NOMER_ORDER");
        $objPHPExcel->getActiveSheet()->setCellValue('A6', $order->NAMA_SUPLIER);
        $objPHPExcel->getActiveSheet()->setCellValue('F6', "Tanggal : $order->TGL_ORDER");
        $objPHPExcel->getActiveSheet()->setCellValue('A7', $order->ALAMAT);
        $objPHPExcel->getActiveSheet()->setCellValue('A8', $order->KOTA);
        $objPHPExcel->getActiveSheet()->setCellValue('A9', $order->TELPON1);
        $objPHPExcel->getActiveSheet()->setCellValue('A10', "Nota Pesanan");
        
        $sheet->mergeCells('A5:C5');
        $sheet->mergeCells('A6:C6');
        $sheet->mergeCells('F5:G5');
        $sheet->mergeCells('F6:G6');
        $sheet->mergeCells('A7:C7');
        $sheet->mergeCells('A8:C8');
        $sheet->mergeCells('A9:C9');
        $sheet->mergeCells('A10:G10');

        $objPHPExcel->getActiveSheet()->getStyle('A10')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('A11', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B11', "Kode Barang");
        $objPHPExcel->getActiveSheet()->setCellValue('C11', "Satuan");
        $objPHPExcel->getActiveSheet()->setCellValue('D11', "Nama Barang");
        $objPHPExcel->getActiveSheet()->setCellValue('E11', "Jumlah");
        $objPHPExcel->getActiveSheet()->setCellValue('F11', "Harga/Unit");
        $objPHPExcel->getActiveSheet()->setCellValue('G11', "Sub Total");

        $i = 11;
        foreach($resultdetail->result() as $row) {
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i-11);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row->ID_BARANG);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $row->NAMA_SATUAN);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->NAMA_BARANG);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->JUMLAH);
                $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $row->HARGA_BELI);
                $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $row->TOTAL_HARGA);   
        }
        $j = $i + 1;
        $objPHPExcel->getActiveSheet()->setCellValue("A$j", "Total");
        $sheet->mergeCells("A$j:F$j");
        $objPHPExcel->getActiveSheet()->getStyle("A$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->setCellValue("G$j", "=SUM(G12:G".$i.")");

        $j++;
        $objPHPExcel->getActiveSheet()->setCellValue("A$j", "Balikpapan, $order->TGL_ORDER");
        $sheet->mergeCells("A$j:B$j");
        $objPHPExcel->getActiveSheet()->getStyle("A$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $j++;
        $objPHPExcel->getActiveSheet()->setCellValue("A$j", "Pengawas Toko");
        $sheet->mergeCells("A$j:B$j");
        $objPHPExcel->getActiveSheet()->getStyle("A$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue("C$j", "Bagian Pembelian");
        $sheet->mergeCells("C$j:D$j");
        $objPHPExcel->getActiveSheet()->getStyle("C$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue("E$j", "Suplier");
        $sheet->mergeCells("E$j:G$j");
        $objPHPExcel->getActiveSheet()->getStyle("E$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


        $j+=4;
        $objPHPExcel->getActiveSheet()->setCellValue("A$j", "Pengawas Toko");
        $sheet->mergeCells("A$j:B$j");
        $objPHPExcel->getActiveSheet()->getStyle("A$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue("C$j", "Bagian Pembelian");
        $sheet->mergeCells("C$j:D$j");
        $objPHPExcel->getActiveSheet()->getStyle("C$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue("E$j", $order->NAMA_SUPLIER);
        $sheet->mergeCells("E$j:G$j");
        $objPHPExcel->getActiveSheet()->getStyle("E$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HPO\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="PO.xls"');
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

    

}
?>
